package com.tasty.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tasty.context.BaseContext;
import com.tasty.dto.ShoppingCartDTO;
import com.tasty.entity.Dish;
import com.tasty.entity.Setmeal;
import com.tasty.entity.ShoppingCart;
import com.tasty.mapper.DishMapper;
import com.tasty.mapper.SetmealMapper;
import com.tasty.mapper.ShoppingCartMapper;
import com.tasty.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */

    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //获取userid
        Long userId = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = new ShoppingCart();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,userId)
                .eq(shoppingCartDTO.getDishId() != null,ShoppingCart::getDishId,shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor());

        List<ShoppingCart> list = shoppingCartMapper.selectList(queryWrapper);

        //判断购物车中是否存在相同菜品或套餐
        if (list != null && list.size() > 0){
            BeanUtils.copyProperties(list.get(0),shoppingCart);
            //如果存在，执行update操作，更新菜品或套餐数量
            shoppingCart.setNumber(list.get(0).getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart);

        }else {

            //如果不存在，执行insert操作，将数据存储到数据库中
            Long dishId = shoppingCartDTO.getDishId();
            //当添加的是菜品数据时
            if (dishId != null)
            {
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //当添加的是套餐数据时
            else{
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setUserId(userId);
            shoppingCart.setNumber(1);
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setDishId(shoppingCartDTO.getDishId());
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);


        }

    }

    /**
     * 查看购物车
     * @return
     */

    public List<ShoppingCart> showShoppingCart() {

        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,userId);

        List<ShoppingCart> list = shoppingCartMapper.selectList(wrapper);
        return list;
    }

    /**
     * 清空购物车
     */


    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,userId);

        shoppingCartMapper.delete(wrapper);

    }


    /**
     * 减去购物车中的商品
     * @param shoppingCartDTO
     */

    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,userId)
                .eq(shoppingCartDTO.getDishId() != null,ShoppingCart::getDishId,shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor());

        List<ShoppingCart> list = shoppingCartMapper.selectList(queryWrapper);

        if (list != null && list.size() > 0){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();

            if (number == 1){
                //商品数量为1，删除商品
                shoppingCartMapper.deleteById(shoppingCart);
            }else {
                //商品数量大于1，使数量减一
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }

    }
}



